package com.ruoyi.crm.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.CrmOrder;
import com.ruoyi.crm.mapper.CrmOrderMapper;
import com.ruoyi.crm.service.ICrmOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.CrmOrderTraceMapper;
import com.ruoyi.crm.domain.CrmOrderTrace;
import com.ruoyi.crm.service.ICrmOrderTraceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author yanwenliang
 * @date 2024-10-25
 */
@Service
public class CrmOrderTraceServiceImpl extends ServiceImpl<CrmOrderTraceMapper, CrmOrderTrace> implements ICrmOrderTraceService
{
    @Autowired
    private CrmOrderTraceMapper crmOrderTraceMapper;

    @Autowired
    private ICrmOrderService crmOrderService;

    /**
     * 查询商机跟进记录
     *
     * @param traceOrderId 商机跟进记录主键
     * @return 商机跟进记录
     */
    @Override
    public CrmOrderTrace selectCrmOrderTraceByTraceOrderId(String traceOrderId)
    {
        return crmOrderTraceMapper.selectCrmOrderTraceByTraceOrderId(traceOrderId);
    }

    /**
     * 查询商机跟进记录列表
     *
     * @param crmOrderTrace 商机跟进记录
     * @return 商机跟进记录
     */
    @Override
    public List<CrmOrderTrace> selectCrmOrderTraceList(CrmOrderTrace crmOrderTrace)
    {
        return crmOrderTraceMapper.selectCrmOrderTraceList(crmOrderTrace);
    }

    /**
     * 新增商机跟进记录
     *
     * @param crmOrderTrace 商机跟进记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCrmOrderTrace(CrmOrderTrace crmOrderTrace) {
        crmOrderTrace.setTraceOrderId(IdWorker.getIdStr());
        crmOrderTrace.setTraceOrderBy(SecurityUtils.getUsername());
        crmOrderTrace.setTraceOrderTime(new Date());

        CrmOrder crmOrder = crmOrderService.getById(crmOrderTrace.getOrderId());

        if (ObjectUtils.isEmpty(crmOrder)){
            throw new ServiceException("商机不存在，请刷新后重试！");
        }
        crmOrder.setNextFollowupTime(crmOrderTrace.getTraceOrderNextTime());
        crmOrderService.updateCrmOrderInfo(crmOrder);
        return crmOrderTraceMapper.insertCrmOrderTrace(crmOrderTrace);
    }

    /**
     * 修改商机跟进记录
     *
     * @param crmOrderTrace 商机跟进记录
     * @return 结果
     */
    @Override
    public int updateCrmOrderTrace(CrmOrderTrace crmOrderTrace) {
        return crmOrderTraceMapper.updateCrmOrderTrace(crmOrderTrace);
    }

    /**
     * 批量删除商机跟进记录
     *
     * @param traceOrderIds 需要删除的商机跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmOrderTraceByTraceOrderIds(String[] traceOrderIds)
    {
        return crmOrderTraceMapper.deleteCrmOrderTraceByTraceOrderIds(traceOrderIds);
    }

    /**
     * 删除商机跟进记录信息
     *
     * @param traceOrderId 商机跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmOrderTraceByTraceOrderId(String traceOrderId)
    {
        return crmOrderTraceMapper.deleteCrmOrderTraceByTraceOrderId(traceOrderId);
    }
}
